package Lab1;

import java.io.BufferedReader;
import java.util.Date;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.StringTokenizer;

import se.lth.cs.realtime.RTThread;


public class TCPThread extends RTThread {
		
	private Socket socket;
	//private BufferedReader in;
	BufferedReader brIn;
	private BufferedWriter out;
	
	RegulMon monitor;
	
	//Timing variables
	long lastTimeMeasure = 0;
	long accDelay = 0;
	int numberPackets = 0;
	Date date;
	
	private class packet{
		String type;
		double value;
		StringTokenizer tok;
		packet(String input){
			tok = new StringTokenizer(input, " ");
			if (tok.countTokens() == 2) {
				try {
					this.type = tok.nextToken();
					this.value = Double.parseDouble(tok.nextToken());
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
			} else {
				System.out.println("Error: Invalid number of tokens in string:");
				System.out.println("\"" + input + "\"");
			}
//			System.out.println("[TCPThread]: " + type + " " + value);
		}
	}
	
	public TCPThread(String host, int port, RegulMon monitor) {
		this.monitor = monitor;
		try {
			this.socket = new Socket(host,port);
			this.brIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
		} catch (IOException e) {
			System.out.println("Unable to connect to " + host + ":" + port);
			System.exit(1);
		}
	}
	
	public void shutDown() {
		try {
			System.out.println("Closing connections");
			brIn.close();
			out.close();
			socket.close();
		} catch (IOException e) {
			System.out.println("IOException: " + e.getMessage());
			e.printStackTrace();
		}
	}

	public void run() {
		System.out.println("[TCPThread]: started. Local port: " + socket.getLocalPort());
		try {
			while (true) {
				//Blocks until incoming packet
				parsePacket(new packet(brIn.readLine()));
				
			}
		} catch (Exception e) {
			System.out.println(e);
		}
	}
	
	private void parsePacket(packet paket) {
//		System.out.println("[TCPThread]: packet arrived");
		if (paket.type.compareTo("pos")==0){	
			monitor.setPos(paket.value);
		}
		else if(paket.type.compareTo("angle")==0){
			monitor.setAng(paket.value);
/*			numberPackets++;
			if(lastTimeMeasure == 0){
				lastTimeMeasure = System.currentTimeMillis();
			}else{
				long currentTime = System.currentTimeMillis();
				long currentdelay = currentTime - lastTimeMeasure;
				accDelay += currentdelay;
				lastTimeMeasure = currentTime;
				System.out.println("[TCPThread]: mean delay: " + accDelay/(numberPackets-1));
				System.out.println("[TCPThread]: last delay: " + currentdelay);
				
			}
	*/		
		}
		else if(paket.type.compareTo("angleRef")==0)
			monitor.setAngleRef(paket.value);
		else if(paket.type.compareTo("posRef")==0)
			monitor.setPosRef(paket.value);
	}

	private void DebugPrint(String message) {
		//System.out.println(message);
	}
}
